#include <iostream>
#include <cstring>

using namespace std;
const int N = 810;
bool st[N];
int a[N][N];

int main()
{
	int T; cin >> T;
	while(T--)
	{	
		memset(st, 0, sizeof st);
		int n; cin >> n;
		for(int i = 1; i <= n; i++)
		{
			for(int j = 1; j <= n; j++)
			{
				cin >> a[i][j];
				st[a[i][j]] = true;
			}
		}
		for(int i = 1; i <= 2 * n; i++)
		{
			if(st[i] == false) 
			{
//				cout <<1 << endl;
				cout << i << " ";
				break;
			}
		}
		for(int i = 1; i <= n; i++)
		{
			for(int j = 1; j <= n; j++)
			{
				if(i == 1) cout << a[i][j] << " ";
				else if(j == n) cout << a[i][j] << " ";
			}
		}
		cout << endl;
	}
	return 0;
}
